<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	.submit{
		text-align: center;
	}
	.widget-box{
		margin-top: -10px;
	}
	#edit-application{
		margin-top: 2vh;
		margin-left: 16vw;
	}
</style>
	<link rel="stylesheet" href="<?=$staticAdminUrl?>assets/css/workflow.css" />
	<div class="page-header">
		<h1>修改申请编号：<?= $applicationInfo['id'] ?></h1>
	</div>

	<div class="row">
		<div class="col-sm-10">
			<div class="widget-box transparent">
				<form id="workflow-design-form" class="form-horizontal">
					<div class="workflow-design-form-bottom"></div>
					<div class="submit">
						<a href="javascript:;" class="btn btn-sm btn-primary" id="edit-application">
							<i class="ace-icon fa fa-check"></i> 提交
						</a>
					</div>
				</form>
			</div>
		</div>
	</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<!-- 百度编辑器 -->
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.config.js"></script>
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.all.js"></script>
<!-- 表单js -->
<script src="<?=$staticAdminUrl?>assets/js/workflow.js"></script>
<script type="text/javascript">
	$(function(){
		// 初始化表单内容
		$(document).editWorkflowApplication({
			"droppableDom": $("#workflow-design-form"),
			"formData": <?= $applicationInfo['content'] ? $applicationInfo['content'] : "null" ?>,
			"callback": function(formData){
				// 保存表单结构	
				var actionUrl = "<?= admin_url('workflow/save_edit_application/' . $applicationInfo['id'] ) ?>";
				var params = {
					"application_form": formData
				};
				ajax_post(actionUrl, params, function(res) {
					window.location.href = "<?=admin_url('workflow/my_create')?>";
				})
			}
        })
        // 时间选择控件
        var timeformat = 'YYYY-MM-DD HH:mm:ss';
		if($(".time-input").eq(0).attr('data-timeformat')){
			timeformat = $(".time-input").eq(0).attr('data-timeformat');
		}
		$(".time-input").datetimepicker({
			format: timeformat
		})
		// 上传对象
		var uploadTarget = $("#upload-file");
		// 上传附件
		uploadTarget.uploader({
			"server":  '<?=UP_FORM_URL?>',
			"authorization": "<?=$signature?>",
			"policy": "<?=$policy?>",
			"accept": "file",
			"callback": function(response) {
				// 添加附件
				var fileName = decodeURIComponent(response.name);
				var fileType = response.type;
				var fileLink = upyunUrl + response.url;
				add_attachment(fileName, fileType, fileLink);
				// 添加附件
				save_attachment();
			}
		});
		// 删除附件
		$("body").on("click", ".attachment-file a.remove-file", function(){
			var me = $(this);
			layer.confirm('确定删除该附件吗？', function(){
				me.closest(".attachment-file").remove();
				layer.closeAll();
				// 重新保存附件
                save_attachment();
				// 重新上传对象
				var uploadTarget = $("#upload-file");
				// 上传附件
				uploadTarget.uploader({
					"server":  '<?=UP_FORM_URL?>',
					"authorization": "<?=$signature?>",
					"policy": "<?=$policy?>",
					"accept": "file",
					"callback": function(response) {
						// 添加附件
						var fileName = decodeURIComponent(response.name);
						var fileType = response.type;
						var fileLink = upyunUrl + response.url;
						add_attachment(fileName, fileType, fileLink);
						// 添加附件
						save_attachment();
					}
				});
			})
		})
    })
	// 添加附件
	function add_attachment(fileName, fileType, fileLink){
		$("#file-div").append('<li class="attachment-file" data-name="'+ fileName +'" data-type="'+ fileType +'" data-link="'+ fileLink +'">\
			<a href="javascript:;" class="attached-file">\
				<i class="ace-icon fa fa-film bigger-110"></i>\
				<span class="attached-name">'+ fileName +'</span>\
			</a>\
			<span class="action-buttons" style="position:relative; top:3px;">\
				<a href="'+ fileLink +'" target="_blank">\
					<i class="ace-icon fa fa-download bigger-125 blue"></i>\
				</a>\
				<a href="javascript:;" class="remove-file">\
					<i class="ace-icon fa fa-trash-o bigger-125 red"></i>\
				</a>\
			</span>\
		</li>')
	}

	// 保存附件
	function save_attachment(){
		var attachmentArray = new Array();
		$(".attachment-file").each(function(){
			var file = {
				"name": $(this).data("name"),
				"type": $(this).data("type"),
				"link": $(this).data("link")
			};
			attachmentArray.push(file);
		})
		$("[name=mail_attachment]").val(JSON.stringify(attachmentArray));
	}
</script>
